<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/1/14
 * Time: 11:49
 */

namespace app\common\service;

use yii\base\Exception;
use yii\helpers\Json;

class EdmService
{
    const API_TOKEN = 'gtDChxdzxhKeqkUCMmAqheIVhS49Jsx8BxtDUPzW5gkLqMEfCb8PHmSASNZX';
    const HOST = 'https://mailer.vikoc.com';
    const API_LIST = '/api/v1/lists';
    const API_SUBSCRIBERS = '/api/v1/subscribers';
    const API_CAMPAIGNS = '/api/v1/campaigns';

    public $err;

    public function getListParams($email){
        $name = date("Ymd")."SendTiktokProductToCreator";
        $params = [
            'api_token'=> self::API_TOKEN,
            'name'=> $name,
            'from_email'=> $email,
            'from_name'=> $email,
            'default_subject'=> $email,
            'contact' => [
                'company'=> "VIKOC",
                'address_1'=> "Globle center",
                'address_2'=> "1713",
                'city'=> "ChengDu",
                'country_id'=> "43",
                'zip'=> "610000",
                'state'=> "SiChuan",
                'phone'=> "+1 5099570885",
                'email'=> $email,
            ],
        ];
        return $params;
    }

    public function getUrl($url){
        return self::HOST . $url;
    }

    public function createList($email){
        $params = $this->getListParams($email);
        return $this->post($this->getUrl(self::API_LIST),$params);
    }


    public function getSubscribersParams($list_uid,$email){
        return [
            'api_token'=> self::API_TOKEN,
            'list_uid'=> $list_uid,
            'EMAIL'=> $email,
            'tag'=> $list_uid,
            'FIRST_NAME'=> $email,
            'LAST_NAME'=> $email
        ];
    }

    public function createSubscribers($list_uid,$email){
        $params = $this->getSubscribersParams($list_uid,$email);
        return $this->post($this->getUrl(self::API_SUBSCRIBERS),$params);
    }


    public function getCampaignsParams($name,$mail_list_uid,$email,$run_at,$title,$email_content){
        return [
            'api_token'=> self::API_TOKEN,
            'name'=> $name,
            'mail_list_uid'=> $mail_list_uid,
            'from_name'=> 'VIKOC',
            'reply_to'=> $email,
            'run_at'=> $run_at,
            'subject'=> $title,
            'from_email'=> $email,
            'email_content'=> $email_content,
        ];
    }

    public function createCampaigns($name,$mail_list_uid,$email,$run_at,$title,$email_content){
        $params = $this->getCampaignsParams($name,$mail_list_uid,$email,$run_at,$title,$email_content);
        return $this->post($this->getUrl(self::API_CAMPAIGNS),$params);
    }



    public function post($url,$params){

        // 初始化cURL会话
        $ch = curl_init();

        // 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url); // 目标URL
        curl_setopt($ch, CURLOPT_POST, true); // 发送POST请求
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // 设置头部信息
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应而不是输出


        $jsonData = Json::encode($params); // 将数组编码为JSON

        // 设置POST数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

        // 执行cURL会话
        $response = curl_exec($ch);

        // 检查是否有错误发生
        if(curl_errno($ch)){
            $this->err = curl_error($ch);
            return false;
        }

        // 关闭cURL会话
        curl_close($ch);

        try{
            // 打印响应
            return Json::decode($response,true);
        }catch (\Throwable $e){
            throw new Exception($e->getMessage());
        }


    }
}